/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zwu.tomcatspace;

import java.io.File;
import java.net.URL;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.observation.Event;
import javax.jcr.observation.EventIterator;
import javax.jcr.observation.EventListener;
import javax.jcr.observation.ObservationManager;
import org.apache.jackrabbit.core.TransientRepository;
import org.apache.jackrabbit.core.query.lucene.Import;
import org.apache.log4j.Logger;

/**
 *
 * @author zwu
 */
public class MetadataListener implements EventListener {

    Session session;
    Logger _logger;

    MetadataListener(Session session) {
        this.session = session;
        _logger = Logger.getLogger(this.getClass());
    }

    public void onEvent(EventIterator eventIterator) {

        try {
            while (eventIterator.hasNext()) {
                Event event = eventIterator.nextEvent();
                String eventPath = event.getPath();
                int eventType = event.getType();
                if (eventType == Event.NODE_ADDED) {
                    String nodePath = eventPath.substring(1, eventPath.length());

                    Node metaEntryNode = session.getRootNode().getNode(nodePath);

                    System.out.println("event..."+nodePath + " node type" + metaEntryNode.getPrimaryItem());
                    _logger.debug(nodePath);
                    /*
                    if(metaEntryNode.getName().equals("blogEntry")){
                    _logger.debug("New blog entry is added by"
                    + metaEntryNode.getProperty(
                    JackrabbitBlogEntryDAO.PROP_BLOGAUTHOR)
                    .getString()
                    + ", titled "
                    + metaEntryNode.getProperty(
                    JackrabbitBlogEntryDAO.PROP_TITLE)
                    .getString());
                    }*/
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        System.out.println(new File(".").getAbsolutePath());
        Repository repository = new TransientRepository(new File("data/tomcat"));
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));


        try {
            //testQueryExcept(session.getWorkspace());

            ObservationManager observationManager =
                    session.getWorkspace().getObservationManager();
            observationManager.addEventListener(new MetadataListener(session),
                    Event.NODE_ADDED, "/", true, null, null, false);

            Node node = session.getRootNode().addNode("event");
            session.save();
            Import.importing(session.getWorkspace(), new URL("http://www.day.com/maven/jsr170/javadocs/jcr-1.0/javax/jcr/observation/EventListener.html"));
            session.save();
            //session.refresh(true);
            Thread.currentThread().sleep(9000);
           
            session.save();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.logout();
        }

    }
}
